<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2015/12/14
 * Time: 16:11
 */

namespace Home\Controller;
use Common\Util\WeiUtil;
use Home\Model\PointlogModel;
use Think\Controller;

class PayController extends Controller
{
    private $wei;
    private $sp;//产品信息
    private $point;//积分日志
    private $goods;//商品信息
    private $money;//金额
    private $upoint;//使用的积分
    function _initialize(){
        $this->wei=new WeiUtil();
        $this->sp=M('sp');
        $this->point=D('Pointlog');
        $this->goods=array();
        $this->money=0;
    }
      public function index(){
          $datas=array(
              'body'=>'test',
              'attach'=>'test',
              'total_fee'=>"1",
              'goods_tag'=>'test',
              'notify_url'=>'http://wq.duguying.net/weixin/example/notify.php'
          );
          $code=I('code',0);
          $point=I('point',0);
          $data=array();
          foreach($code as $v){
              $where['bh']=$v;
              $this->goods[]=$this->sp->where($where)->find();
          }
          if($this->goods){
              $this->money=0;
              foreach($this->goods as $val){
                  $this->money+=floatval($val['monay']);
              }
              session("user",array('id'=>2));
              $user=session("user");
              $where['id']=$user['id'];
              $info=D('User')->getOne($where);
              if($info && $info['jifen']>0){
                  if($info['jifen']>$point){
                     //$this->point->log($user['id'],$point,"购买商品使用了".$point."积分",PointlogModel::DEC);
                      //$jsApiParameters=$this->wei->jspay($data);
                      $this->upoint=$point;
                      $this->success("有积分");
                  }else{
                      $this->error("积分不足");
                  }
              }
              $this->success("未使用积分");
              //$jsApiParameters=$this->wei->jspay($datas);
              //$this->success($jsApiParameters);
              //$this->success(session('openId'));
          }else{
              $this->error("请提交数据");
          }
      }
      public function show()
      {
          $code = I('code', 0);
          $point = I('point', 0);
          $data = array();
          $code = explode(',', $code);
          foreach ($code as $v) {
              $where['bh'] = $v;
              $goods[] = $this->sp->where($where)->find();
          }
          dump($point);
          dump($code);
          dump($goods);
          if ($goods) {
              $money = 0;
              foreach ($goods as $val) {
                  $money += floatval($val['monay']);
              }
              session("user", array('id' => 2));
              $user = session("user");
              $where['id'] = $user['id'];
              $info = D('User')->getOne($where);
              if ($info && $info['jifen'] > 0) {
                  if ($info['jifen'] > $point) {
                      //$this->point->log($user['id'],$point,"购买商品使用了".$point."积分",PointlogModel::DEC);
                      //$jsApiParameters=$this->wei->jspay($data);
                      $datas = array(
                          'body' => 'test',
                          'attach' => 'test',
                          'total_fee' => "1",
                          'goods_tag' => 'test',
                          'notify_url' => 'http://wq.duguying.net/weixin/example/notify.php'
                      );
                      $jsApiParameters = $this->wei->jspay($datas);
                      dump($jsApiParameters);
                  } else {
                      $this->error("积分不足");
                  }
              }
          }
      }
}